<?php

namespace App\Jobs;

use App\Models\Posts\WxComment;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxTag;
use App\Models\Shop\WxShopGoods;
use App\Models\User\WxUser;
use App\Models\WxSearch;
use App\Wen\Utils\UserUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;

class AttachIpAddressJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $type = 0;
    protected $object_id = 0;
    public $tries = 1;


    /**
     * AttachIpAddressJob constructor.
     * @param $ip
     * @param $type 1: 笔记 2：用户 3：评论 4：搜索 5：话题 6：商品
     * @param $object_id
     */
    public function __construct($type, $object_id)
    {
        //
        $this->type = $type;
        $this->object_id = $object_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(_empty_($this->type) || _empty_($this->object_id)){
            return;
        }
        $object_ip = '';
        $user_id = 0;
        if($this->type == 1){
            $object_ip = WxPost::where('id', $this->object_id)->value('ip');
            $user_id = WxPost::where('id', $this->object_id)->value('user_id');
        }else if($this->type == 2){
            $object_ip = WxUser::where('id', $this->object_id)->value('ip');
            $user_id = $this->object_id;
        }else if($this->type == 3){
            $object_ip = WxComment::where('id', $this->object_id)->value('ip');
            $user_id = WxComment::where('id', $this->object_id)->value('user_id');
        }else if($this->type == 4){
            $object_ip = WxSearch::where('id', $this->object_id)->value('ip');
            $user_id = WxSearch::where('id', $this->object_id)->value('user_id');
        }else if($this->type == 5){
            $object_ip = WxTag::where('id', $this->object_id)->value('ip');
            $user_id = WxTag::where('id', $this->object_id)->value('user_id');
        }else if($this->type == 6){
            $object_ip = WxShopGoods::where('id', $this->object_id)->value('ip');
            $user_id = 0;
        }

        if(_empty_($object_ip)){
            return;
        }

        $res = _ip_address($object_ip);
        if(_empty_($res)){
            return;
        }

        // 1: 笔记 2：用户 3：评论 4：搜索 5：话题 6：商品
        $res__ = [];
        if(_empty_($res['longitude']) && _empty_($res['latitude'])){
            return;
        }

        $res__['longitude'] = $res['longitude'];
        $res__['latitude'] = $res['latitude'];

        if(!_empty_($res['country'])){
            $res__['country'] = $res['country'];
        }
        if(!_empty_($res['province'])){
            $res__['province'] = $res['province'];
        }

        if(!_empty_($res['city'])){
            $res__['city'] = $res['city'];
        }

        if(!_empty_($res['district'])){
            $res__['district'] = $res['district'];
        }

        if(env('APP_URL') == 'https://mini.minisns.cn'){
            if(UserUtils::is_mini_admin($user_id)){
                $res__['longitude'] = '120.63212';
                $res__['latitude'] = '31.26249';
                $res__['province'] = '江苏';
                $res__['city'] = '苏州市';
                $res__['district'] = '吴中区';
            }
        }

        if($this->type == 1){
            WxPost::where('id', $this->object_id)->update($res__);
        }else if($this->type == 2){
            WxUser::where('id', $this->object_id)->update($res__);
            Cache::forget('get:cached:user:'.$this->object_id);
        }else if($this->type == 3){
            WxComment::where('id', $this->object_id)->update($res__);
        }else if($this->type == 4){
            WxSearch::where('id', $this->object_id)->update($res__);
        }else if($this->type == 5){
            WxTag::where('id', $this->object_id)->update($res__);
        }else if($this->type == 6){
            WxShopGoods::where('id', $this->object_id)->update($res__);
        }
    }
}
